//
//  main.m
//  04-
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"
int main(int argc, const char * argv[]) {
   
    NSString *path = @"/Users/dllo/Documents/04-/04-/crayons.txt";
    NSString *sourceStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
   // NSLog(@"%@",sourceStr);
    // 根据\n切割字符串
    NSArray *strArray = [sourceStr componentsSeparatedByString:@"\n"];
    // 声明存储颜色的可变字典
    NSMutableDictionary *colorDictionary = [NSMutableDictionary dictionary];
    for (int i = 0; i < strArray.count; i++) {
        if ([strArray[i] length] > 0) {
            NSArray *color = [strArray[i]componentsSeparatedByString:@" #"];
            [colorDictionary setObject:[color lastObject] forKey:[color firstObject]];
        }
    }
    //2.
    NSMutableDictionary *newDic = [NSMutableDictionary dictionary];
    NSArray *allkeys = colorDictionary.allKeys;
    for (int i = 0; i < allkeys.count; i++) {
             // 取首字母
        NSString *subStr = [allkeys[i] substringToIndex:1];
        // NSLog(@"%@",subStr);
        // 通过首字母取出对应 array
        NSMutableArray *colorArray = [newDic objectForKey:subStr];
        // 根据老字典的 key 和 value 生成新的Color对象
        Color *newColor = [[Color alloc] init];
        newColor.colorName = allkeys[i];
        newColor.colorValue = [colorDictionary objectForKey:allkeys[i]];
        // 判断,从新字典取出的数组是否为空
        // 为空说明,还没有存过对应字母的首字母的数组
        if (colorArray == nil) {
            colorArray = [NSMutableArray arrayWithObject:newColor];
            //赋值给对应首字母
            [newDic setObject:colorArray forKey:subStr];
        } else {
            [colorArray addObject:newColor];
            [newDic setObject:colorArray forKey:subStr];
        }
           }
  NSLog(@"%@",newDic);

    return 0;
}
